home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / fmmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  6.5 KB  |  334 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Font Manager module */
  33.  
  34. #include "allobjects.h"
  35.  
  36. #include "modsupport.h"
  37.  
  38. #include <gl.h>
  39. #include <device.h>
  40. #include <fmclient.h>
  41.  
  42.  
  43. /* Font Handle object implementation */
  44.  
  45. typedef struct {
  46.     OB_HEAD
  47.     fmfonthandle fh_fh;
  48. } fhobject;
  49.  
  50. staticforward typeobject Fhtype;
  51.  
  52. #define is_fhobject(v)        ((v)->ob_type == &Fhtype)
  53.  
  54. static object *
  55. newfhobject(fh)
  56.     fmfonthandle fh;
  57. {
  58.     fhobject *fhp;
  59.     if (fh == NULL) {
  60.         err_setstr(RuntimeError, "error creating new font handle");
  61.         return NULL;
  62.     }
  63.     fhp = NEWOBJ(fhobject, &Fhtype);
  64.     if (fhp == NULL)
  65.         return NULL;
  66.     fhp->fh_fh = fh;
  67.     return (object *)fhp;
  68. }
  69.  
  70. /* Font Handle methods */
  71.  
  72. static object *
  73. fh_scalefont(self, args)
  74.     fhobject *self;
  75.     object *args;
  76. {
  77.     double size;
  78.     if (!getargs(args, "d", &size))
  79.         return NULL;
  80.     return newfhobject(fmscalefont(self->fh_fh, size));
  81. }
  82.  
  83. /* XXX fmmakefont */
  84.  
  85. static object *
  86. fh_setfont(self, args)
  87.     fhobject *self;
  88.     object *args;
  89. {
  90.     if (!getnoarg(args))
  91.         return NULL;
  92.     fmsetfont(self->fh_fh);
  93.     INCREF(None);
  94.     return None;
  95. }
  96.  
  97. static object *
  98. fh_getfontname(self, args)
  99.     fhobject *self;
  100.     object *args;
  101. {
  102.     char fontname[256];
  103.     int len;
  104.     if (!getnoarg(args))
  105.         return NULL;
  106.     len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
  107.     if (len < 0) {
  108.         err_setstr(RuntimeError, "error in fmgetfontname");
  109.         return NULL;
  110.     }
  111.     return newsizedstringobject(fontname, len);
  112. }
  113.  
  114. static object *
  115. fh_getcomment(self, args)
  116.     fhobject *self;
  117.     object *args;
  118. {
  119.     char comment[256];
  120.     int len;
  121.     if (!getnoarg(args))
  122.         return NULL;
  123.     len = fmgetcomment(self->fh_fh, sizeof comment, comment);
  124.     if (len < 0) {
  125.         err_setstr(RuntimeError, "error in fmgetcomment");
  126.         return NULL;
  127.     }
  128.     return newsizedstringobject(comment, len);
  129. }
  130.  
  131. static object *
  132. fh_getfontinfo(self, args)
  133.     fhobject *self;
  134.     object *args;
  135. {
  136.     fmfontinfo info;
  137.     object *v;
  138.     if (!getnoarg(args))
  139.         return NULL;
  140.     if (fmgetfontinfo(self->fh_fh, &info) < 0) {
  141.         err_setstr(RuntimeError, "error in fmgetfontinfo");
  142.         return NULL;
  143.     }
  144.     return mkvalue("(llllllll)",
  145.                info.printermatched,
  146.                info.fixed_width,
  147.                info.xorig,
  148.                info.yorig,
  149.                info.xsize,
  150.                info.ysize,
  151.                info.height,
  152.                info.nglyphs);
  153. }
  154.  
  155. #if 0
  156. static object *
  157. fh_getwholemetrics(self, args)
  158.     fhobject *self;
  159.     object *args;
  160. {
  161. }
  162. #endif
  163.  
  164. static object *
  165. fh_getstrwidth(self, args)
  166.     fhobject *self;
  167.     object *args;
  168. {
  169.     char *str;
  170.     if (!getstrarg(args, &str))
  171.         return NULL;
  172.     return newintobject(fmgetstrwidth(self->fh_fh, str));
  173. }
  174.  
  175. static struct methodlist fh_methods[] = {
  176.     {"scalefont",    (method)fh_scalefont},
  177.     {"setfont",    (method)fh_setfont},
  178.     {"getfontname",    (method)fh_getfontname},
  179.     {"getcomment",    (method)fh_getcomment},
  180.     {"getfontinfo",    (method)fh_getfontinfo},
  181. #if 0
  182.     {"getwholemetrics",    (method)fh_getwholemetrics},
  183. #endif
  184.     {"getstrwidth",    (method)fh_getstrwidth},
  185.     {NULL,        NULL}        /* sentinel */
  186. };
  187.  
  188. static object *
  189. fh_getattr(fhp, name)
  190.     fhobject *fhp;
  191.     char *name;
  192. {
  193.     return findmethod(fh_methods, (object *)fhp, name);
  194. }
  195.  
  196. static void
  197. fh_dealloc(fhp)
  198.     fhobject *fhp;
  199. {
  200.     fmfreefont(fhp->fh_fh);
  201.     DEL(fhp);
  202. }
  203.  
  204. static typeobject Fhtype = {
  205.     OB_HEAD_INIT(&Typetype)
  206.     0,                /*ob_size*/
  207.     "font handle",            /*tp_name*/
  208.     sizeof(fhobject),        /*tp_size*/
  209.     0,                /*tp_itemsize*/
  210.     /* methods */
  211.     (destructor)fh_dealloc,        /*tp_dealloc*/
  212.     0,                /*tp_print*/
  213.     (getattrfunc)fh_getattr,    /*tp_getattr*/
  214.     0,                /*tp_setattr*/
  215.     0,                /*tp_compare*/
  216.     0,                /*tp_repr*/
  217. };
  218.  
  219.  
  220. /* Font Manager functions */
  221.  
  222. static object *
  223. fm_init(self, args)
  224.     object *self, *args;
  225. {
  226.     if (!getnoarg(args))
  227.         return NULL;
  228.     fminit();
  229.     INCREF(None);
  230.     return None;
  231. }
  232.  
  233. static object *
  234. fm_findfont(self, args)
  235.     object *self, *args;
  236. {
  237.     char *str;
  238.     if (!getstrarg(args, &str))
  239.         return NULL;
  240.     return newfhobject(fmfindfont(str));
  241. }
  242.  
  243. static object *
  244. fm_prstr(self, args)
  245.     object *self, *args;
  246. {
  247.     char *str;
  248.     if (!getstrarg(args, &str))
  249.         return NULL;
  250.     fmprstr(str);
  251.     INCREF(None);
  252.     return None;
  253. }
  254.  
  255. /* XXX This uses a global variable as temporary! Not re-entrant! */
  256.  
  257. static object *fontlist;
  258.  
  259. static void
  260. clientproc(fontname)
  261.     char *fontname;
  262. {
  263.     int err;
  264.     object *v;
  265.     if (fontlist == NULL)
  266.         return;
  267.     v = newstringobject(fontname);
  268.     if (v == NULL)
  269.         err = -1;
  270.     else {
  271.         err = addlistitem(fontlist, v);
  272.         DECREF(v);
  273.     }
  274.     if (err != 0) {
  275.         DECREF(fontlist);
  276.         fontlist = NULL;
  277.     }
  278. }
  279.  
  280. static object *
  281. fm_enumerate(self, args)
  282.     object *self, *args;
  283. {
  284.     object *res;
  285.     if (!getnoarg(args))
  286.         return NULL;
  287.     fontlist = newlistobject(0);
  288.     if (fontlist == NULL)
  289.         return NULL;
  290.     fmenumerate(clientproc);
  291.     res = fontlist;
  292.     fontlist = NULL;
  293.     return res;
  294. }
  295.  
  296. static object *
  297. fm_setpath(self, args)
  298.     object *self, *args;
  299. {
  300.     char *str;
  301.     if (!getstrarg(args, &str))
  302.         return NULL;
  303.     fmsetpath(str);
  304.     INCREF(None);
  305.     return None;
  306. }
  307.  
  308. static object *
  309. fm_fontpath(self, args)
  310.     object *self, *args;
  311. {
  312.     if (!getnoarg(args))
  313.         return NULL;
  314.     return newstringobject(fmfontpath());
  315. }
  316.  
  317. static struct methodlist fm_methods[] = {
  318.     {"init",    fm_init},
  319.     {"findfont",    fm_findfont},
  320.     {"enumerate",    fm_enumerate},
  321.     {"prstr",    fm_prstr},
  322.     {"setpath",    fm_setpath},
  323.     {"fontpath",    fm_fontpath},
  324.     {NULL,        NULL}        /* sentinel */
  325. };
  326.  
  327.  
  328. void
  329. initfm()
  330. {
  331.     initmodule("fm", fm_methods);
  332.     fminit();
  333. }
  334.